
package com.kugou.game.sdk.ui.widget;

import com.kugou.game.sdk.api.common.User;
import com.kugou.game.sdk.base.BaseCommonTitleFragmentActivity;
import com.kugou.game.sdk.core.SDKControler;
import com.kugou.game.sdk.core.UserManager;
import com.kugou.game.sdk.persistence.preference.PrefUtil;
import com.kugou.game.sdk.statistics.StatCmd;
import com.kugou.game.sdk.statistics.StaticsManager;
import com.kugou.game.sdk.ui.activity.CommonSdkActivity;
import com.kugou.game.sdk.ui.activity.RegisterByPhoneActivity;
import com.kugou.game.sdk.ui.fragment.SetLoginPsdFragment;
import com.kugou.game.sdk.utils.Rx;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;

public class ToolBarCustomView extends LinearLayout implements OnClickListener {
    private Context context;

    private ImageView imgUserCenter, imgGameCenter, ivGiftBag, ivGameForum, imgSetLoginPsd;

    private ToolBar toolBar;

    private ViewGroup mGamecenterLayout;

    private View viewLeft, viewRight, viewLeftLine, viewRightLine, giftBagLayout, giftBagLinkLine,
            setLoginPsdLayout, viewSetloginPsdLine;

    public ToolBarCustomView(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.context = context;
        LayoutInflater.from(context).inflate(Rx.layout.kg_toolbar_customview, this);
        initView();
    }

    public ToolBarCustomView(Context context) {
        super(context);
        this.context = context;
        LayoutInflater.from(context).inflate(Rx.layout.kg_toolbar_customview, this);
        initView();
    }

    User mUser;

    private void initView() {
        mUser = UserManager.getInstance().getCurrentUserInternal();

        viewRightLine = findViewById(Rx.id.kg_view_right_line);
        viewLeftLine = findViewById(Rx.id.kg_view_left_line);
        viewLeft = findViewById(Rx.id.kg_view_left);
        viewRight = findViewById(Rx.id.kg_view_right);
        viewSetloginPsdLine = findViewById(Rx.id.kg_set_login_psd_line);
        imgSetLoginPsd = (ImageView) findViewById(Rx.id.kg_iv_set_login_psd);
        imgUserCenter = (ImageView) findViewById(Rx.id.kg_img_user_center);
        imgGameCenter = (ImageView) findViewById(Rx.id.kg_img_game_center);
        ivGiftBag = (ImageView) findViewById(Rx.id.kg_iv_gift_bag);
        ivGameForum = (ImageView) findViewById(Rx.id.kg_iv_game_forum);
        mGamecenterLayout = (ViewGroup) findViewById(Rx.id.kg_game_center_layout);
        giftBagLayout = findViewById(Rx.id.kg_layout_gift_bag);
        setLoginPsdLayout = findViewById(Rx.id.kg_set_login_psd_layout);
        giftBagLinkLine = findViewById(Rx.id.kg_toolbar_giftbag_link_line);
        updateUsercenterIndicatorBg();
        setListener();
        // 是否需要隐藏游戏中心
        if (SDKControler.isNeedHideGameCenter()) {
            mGamecenterLayout.setVisibility(View.GONE);
        }
    }

    private void setListener() {
        imgUserCenter.setOnClickListener(this);
        imgGameCenter.setOnClickListener(this);
        ivGiftBag.setOnClickListener(this);
        ivGameForum.setOnClickListener(this);
        imgSetLoginPsd.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        if (toolBar != null) {
            toolBar.setCustomViewVisibility(View.GONE, true);
        }

        if (v == imgUserCenter) {
            SDKControler.enterUserCenter((Activity) context);
            if (mUser != null && mUser.getUnreadmails() > 0 && !mUser.isUnreadMailReminded()) {
                // 设置当前新邮件已经提醒，可以取消红点了
                mUser.setUnreadMailReminded(true);
                toolBar.updateIndicatorBg();
                updateUsercenterIndicatorBg();
            }

            // 发送统计--从悬浮窗点击进入用户中心
            StaticsManager.sendCommonStatis(StatCmd.CMD_ENTER_USERCENTER_FROM_TOOLBAR);
        } else if (v == imgGameCenter) {
            SDKControler.enterGameCenter((Activity) context);
            // 取消显示红点标识
            if (mUser != null && mUser.isGameCenterNeedRedPointRemind()) {
                mUser.setGameCenterNeedRedPointRemind(false);
                PrefUtil.setGamecenterRedpointData(getContext(),
                        PrefUtil.getGamecenterRedpointDataTemp(getContext()));

                toolBar.updateIndicatorBg();
                updateUsercenterIndicatorBg();
            }
            // 发送统计--从悬浮窗点击进入游戏中心
            StaticsManager.sendCommonStatis(StatCmd.CMD_ENTER_GAME_CENTER_FROM_TOOLBAR);
        } else if (v == ivGiftBag) {
            SDKControler.enterGiftBagCenter((Activity) context);
            // 发送统计--从悬浮窗点击进入礼包列表
            StaticsManager.sendCommonStatis(StatCmd.CMD_ENTER_GIFTBAG_FROM_TOOLBAR);
        } else if (v == ivGameForum) {
            if (mUser != null) {
                if (!mUser.isSetKugouPsd()) {
                    // 跳转到设置登录密码界面
                    context.startActivity(CommonSdkActivity.creatIntent(context,
                            SetLoginPsdFragment.class.getName(), Rx.string.kg_set_login_psd));
                } else {
                    if (mUser.isRegistered()) {
                        SDKControler.enterGameForum((Activity) context);
                    } else {
                        Intent intent = new Intent(getContext(), RegisterByPhoneActivity.class);
                        intent.putExtra(BaseCommonTitleFragmentActivity.FROM_ACTION_KEY,
                                BaseCommonTitleFragmentActivity.FROM_FORUM);
                        getContext().startActivity(intent);
                    }
                }
            } else {
                Intent intent = new Intent(getContext(), RegisterByPhoneActivity.class);
                intent.putExtra(BaseCommonTitleFragmentActivity.FROM_ACTION_KEY,
                        BaseCommonTitleFragmentActivity.FROM_FORUM);
                getContext().startActivity(intent);
            }
            // 发送统计--悬浮窗点击论坛
            StaticsManager.sendCommonStatis(StatCmd.CMD_ENTER_FORUM_FROM_TOOLBAR);
        } else if (v == imgSetLoginPsd) {
            // 跳转到设置登录密码界面
            context.startActivity(CommonSdkActivity.creatIntent(context,
                    SetLoginPsdFragment.class.getName(), Rx.string.kg_set_login_psd));
        }
    }

    /**
     * 根据视图的左右设置一些view的状态（可见、不可见）
     */
    public void setViewStateByPlace(boolean isLeft) {
        if (isLeft) {
            viewLeftLine.setVisibility(View.VISIBLE);
            viewRightLine.setVisibility(View.GONE);
            viewLeft.setVisibility(View.GONE);
            viewRight.setVisibility(View.VISIBLE);
        } else {
            viewLeftLine.setVisibility(View.GONE);
            viewRightLine.setVisibility(View.VISIBLE);
            viewLeft.setVisibility(View.VISIBLE);
            viewRight.setVisibility(View.GONE);
        }
    }

    public ToolBar getToolBar() {
        return toolBar;
    }

    public void setToolBar(ToolBar toolBar) {
        this.toolBar = toolBar;
    }

    /**
     * 根据是否有未读消息，和本次启动是否已经点击过红点提示，决定悬浮窗展开的用户中心图标是否显示红点背景
     */
    public void updateUsercenterIndicatorBg() {
        if (mUser == null) {// 防止CP在未登录前就调用悬浮窗
            return;
        }
        // 用户中心图标
        if (mUser.getUnreadmails() > 0 && !mUser.isUnreadMailReminded()) {
            imgUserCenter.setBackgroundResource(Rx.drawable.kg_toolbar_user_center_red_point);
        } else {
            imgUserCenter.setBackgroundResource(Rx.drawable.kg_toolbar_user_center_seletor);
        }
        // 游戏中心图标
        if (mUser.isGameCenterNeedRedPointRemind()) {
            imgGameCenter.setBackgroundResource(Rx.drawable.kg_toolbar_game_center_red_point);
        } else {
            imgGameCenter.setBackgroundResource(Rx.drawable.kg_toolbar_game_center_seletor);
        }
    }

    public void updateGiftbagIndicatorVisibility() {
        // 没有礼包时，隐藏悬浮窗礼包入口
        if (mUser != null && mUser.isHasGift()) {
            giftBagLayout.setVisibility(View.VISIBLE);
            giftBagLinkLine.setVisibility(View.VISIBLE);
        } else {
            giftBagLayout.setVisibility(View.GONE);
            giftBagLinkLine.setVisibility(View.GONE);
        }
    }

    public void updateSetLoginPsdViewState() {
        if (mUser != null && mUser.isSetKugouPsd()) {
            setLoginPsdLayout.setVisibility(View.GONE);
            viewSetloginPsdLine.setVisibility(View.GONE);
        } else {
            setLoginPsdLayout.setVisibility(View.VISIBLE);
            viewSetloginPsdLine.setVisibility(View.VISIBLE);
        }
    }

}
